﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lon.UI;
using WeifenLuo.WinFormsUI.Docking;
using Lon.UI.Station;
using System.IO;
using Lon.Util;
using Lon.Server;

namespace Lon.UI
{
    public partial class FormAppServer : FormMain
    {
        ToolStripButton btnPrint;
        ToolStripButton btnSave;
        AppServer frontServer;
        FormClientView formClientView;
        ToolStripStatusLabel lblAppSeverState = new ToolStripStatusLabel();
        ToolStripStatusLabel lblLoadBalanceState = new ToolStripStatusLabel();
        ToolStripStatusLabel lblStartTime = new ToolStripStatusLabel();
        ToolStripStatusLabel lblNowTime = new ToolStripStatusLabel();

        public FormAppServer(AppServer frontServer)
        {

            InitializeComponent();
            this.frontServer = frontServer;
            formClientView = new FormClientView(frontServer);
            formClientView.Show(this.dockPanel);

            InitStatusBar(frontServer);

            //   base.statusBar.Items.Clear();





        }



        private void InitStatusBar(AppServer frontServer)
        {
            lblAppSeverState.BorderSides = ToolStripStatusLabelBorderSides.Left;
            lblAppSeverState.BorderStyle = Border3DStyle.Flat;
            lblLoadBalanceState.BorderSides = ToolStripStatusLabelBorderSides.Left;
            lblLoadBalanceState.BorderStyle = Border3DStyle.Flat;
            lblStartTime.BorderSides = ToolStripStatusLabelBorderSides.Left;
            lblStartTime.BorderStyle = Border3DStyle.Flat;
            lblStartTime.Text = "启动时间:" + DateTime.Now.ToString("yy-MM-dd HH:mm:ss");
            lblNowTime.BorderSides = ToolStripStatusLabelBorderSides.Left;
            lblNowTime.TextAlign = ContentAlignment.MiddleRight;
         
            if (frontServer.StandByServer.ClientSocket == null)
            {
                lblLoadBalanceState.Text = "热备服务器:未连接";
                lblLoadBalanceState.BackColor = Color.Red;
            }
            else
            {
                if (frontServer.StandByServer.ClientSocket.Connected)
                {
                    lblLoadBalanceState.Text = "热备服务器已连接  心跳:" + frontServer.StandByServer.LastHeartbeatTime.ToString("HH:mm:ss");
                    lblLoadBalanceState.BackColor = Color.Green;
                }
                else
                {
                    lblLoadBalanceState.Text = "热备服务器未连接";
                    lblLoadBalanceState.BackColor = Color.Red;

                }
            }

            this.statusBar.Items.Add(lblAppSeverState);
            this.statusBar.Items.Add(lblLoadBalanceState);
            this.statusBar.Items.Add(lblStartTime);
            this.statusBar.Items.Add(lblNowTime);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                if (ActiveMdiChild == null) return;
                SaveForm(ActiveMdiChild);

            }
            else if (dockPanel.ActiveDocument == null) return;
            else SaveForm(dockPanel.ActiveDocument);
        }

        void dockPanel_ActiveContentChanged(object sender, EventArgs e)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                if (ActiveMdiChild == null) return;
                ShowRelatedBtn(ActiveMdiChild);

            }
            else if (dockPanel.ActiveDocument == null) return;
            else ShowRelatedBtn(dockPanel.ActiveDocument);

        }

        void btnPrint_Click(object sender, EventArgs e)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                if (ActiveMdiChild == null) return;
                PrintForm(ActiveMdiChild);
            }
            else if (dockPanel.ActiveDocument == null) return;
            else PrintForm(dockPanel.ActiveDocument);
        }

        public void ShowRelatedBtn(object obj)
        {
            if (obj is IPrint)
            {
                if (!tsMain.Items.Contains(btnPrint))
                    tsMain.Items.Add(btnPrint);
            }
            else
            {
                if (tsMain.Items.Contains(btnPrint)) tsMain.Items.Remove(btnPrint);
            }
            if (obj is ISave)
            {
                if (!tsMain.Items.Contains(btnSave))
                    tsMain.Items.Add(btnSave);
            }
            else
            {
                if (tsMain.Items.Contains(btnSave)) tsMain.Items.Remove(btnSave);
            }
        }

        public void PrintForm(object obj)
        {
            if (obj is IPrint)
            {
                ((IPrint)obj).Print();
            }
        }
        public void SaveForm(object obj)
        {
            if (obj is ISave)
            {
                ((ISave)obj).Save();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void StationFormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.msMain.Items.Clear();


            for (int i = 0; i < dockPanel.Contents.Count; i++)
            {
                Form childForm = dockPanel.Contents[i].DockHandler.Form;
                if (childForm == null || childForm.IsDisposed) continue;
                childForm.Close();
            }


        }

        private void StationFormMain_MdiChildActivate(object sender, EventArgs e)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                if (ActiveMdiChild == null) return;
                ShowRelatedBtn(ActiveMdiChild);

            }
            else if (dockPanel.ActiveDocument == null) return;
            else ShowRelatedBtn(dockPanel.ActiveDocument);



        }

        private void StationFormMain_ControlRemoved(object sender, ControlEventArgs e)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                if (ActiveMdiChild == null) return;
                ShowRelatedBtn(ActiveMdiChild);

            }
            else if (dockPanel.ActiveDocument == null) return;
            else ShowRelatedBtn(dockPanel.ActiveDocument);

        }

        private void dockPanel_ActiveContentChanged_1(object sender, EventArgs e)
        {

        }





        FormNetLogView formNetLogView;
        [Cmd]
        public void ShowConnectLog(String[] args)
        {
            if (formNetLogView == null || formNetLogView.IsDisposed)
            {
                formNetLogView = new FormNetLogView();
                formNetLogView.Show();
            }
            else
            {
                formNetLogView.Activate();
            }
        }


        [Cmd]
        public void ShowRawData(String[] args)
        {
            FormRawData frd = new FormRawData(this.frontServer);
            frd.Show(this.dockPanel, DockState.DockBottom);
        }

        private void FormDebug_Shown(object sender, EventArgs e)
        {

        }

        private void FormDebug_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ShowFrontServerState();
            ShowLoadBalanceServerState();
            ShowTimeNow();
        }

        private void ShowTimeNow()
        {
            lblNowTime.Text = "当前时间" + DateTime.Now.ToString("HH:mm:ss");
        }

        private void ShowLoadBalanceServerState()
        {
            if (frontServer.StandByServer.ClientSocket == null) 
            {
                lblLoadBalanceState.Text = "热备服务器:未连接";
                lblLoadBalanceState.BackColor = Color.Red;
            }
            else
            {
                if (frontServer.StandByServer.ClientSocket.Connected)
                {
                    lblLoadBalanceState.Text = "热备服务器已连接  心跳:" + frontServer.StandByServer.LastHeartbeatTime.ToString("HH:mm:ss");
                    lblLoadBalanceState.BackColor = Color.Green;
                }
                else
                {
                    lblLoadBalanceState.Text = "热备服务器未连接";
                    lblLoadBalanceState.BackColor = Color.Red;

                }
            }
        }

     
        private void ShowFrontServerState()
        {
         

        }
    }
}
